#!/bin/sh

[[ $EUID -ne 0 ]] && echo '此脚本需要root权限运行!' && exit 1
found=`grep -c net.ipv4.tcp_tw_recycle /etc/sysctl.conf`

echo "关闭selinux"
/bin/sed -i 's/mingetty tty/mingetty --noclear tty/' /etc/inittab
/bin/sed -i 's/SELINUX=permissive/SELINUX=disabled/' /etc/selinux/config
/bin/sed -i 's/SELINUX=enforcing/SELINUX=disabled/'  /etc/selinux/config
setenforce 0

echo "修改/etc/profile的有关命令前面的]#的内容样式"
/bin/cat<<EOF >> /etc/profile
export PS1='\u@\h:\w\n\\$ '
EOF

echo "关闭一些不常用到的服务 postfix NetworkManager abrt-ccpp"
systemctl disable 'postfix'
systemctl disable 'NetworkManager'
systemctl disable 'abrt-ccpp'

echo "新建sudo用户normalUser并添加到normalUser组，密码为02EAF7CDEDAD4ADB9371FBE8F91DBBE6"
groupadd -g 20000 normalUser
useradd  -g normalUser -u 20000 -s /bin/bash -c "Dev user" -m -d /home/normalUser normalUser
echo 02EAF7CDEDAD4ADB9371FBE8F91DBBE6 | passwd --stdin normalUser
sed -i 's/^Defaults    requiretty/#Defaults    requiretty/' /etc/sudoers
sed -i 's/^Defaults    env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR \\/Defaults    env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR SSH_AUTH_SOCK \\/' /etc/sudoers
cat<<EOF >>/etc/sudoers
# normalUser using sudo
%normalUser        ALL=(ALL)       NOPASSWD: ALL
EOF

echo "调整CPU的能源管理模块优化pstate 和 cstate 数值"
sed -i '/GRUB_CMDLINE_LINUX/{s/"$//g;s/$/ intel_pstate=disable intel_idle.max_cstate=0 processor.max_cstate=1 idle=poll"/}' /etc/default/grub

echo "修改/etc/sysctl.conf优化参数"
if ! [ $found -gt "0" ];then
cat > /etc/sysctl.conf << EOF
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
fs.file-max = 131072
kernel.panic=1
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_sack = 1
net.ipv4.tcp_no_metrics_save = 1
net.core.netdev_max_backlog = 3072
net.ipv4.tcp_max_syn_backlog = 4096
net.ipv4.tcp_max_tw_buckets = 720000
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.tcp_fin_timeout = 5
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_retries1 = 2
net.ipv4.tcp_retries2 = 10
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_syncookies = 1
EOF
fi
sysctl -p

echo "修改/etc/security/limits.conf优化参数，设置操作系统最大的文件打开数"
echo "优化文件句柄的大小，内存里面的空间分配的大小，网卡队列相关的大小，syncookie"
found=`grep -c "^* soft nproc" /etc/security/limits.conf`
if ! [ $found -gt "0" ]
then
cat >> /etc/security/limits.conf << EOF
* soft nproc 2048
* hard nproc 16384
* soft nofile 8192
* hard nofile 65536
EOF
fi

echo "默认的ssh的默认22端口改成 9822 端口"
/bin/sed -i 's/.*Port[[:space:]].*$/Port 9822/' /etc/ssh/ssh_config
/bin/sed -i 's/.*Port[[:space:]].*$/Port 9822/' /etc/ssh/sshd_config
/bin/sed -i 's/port=\"22\"/port=\"9822\"/' /usr/lib/firewalld/services/ssh.xml 
firewall-cmd --reload

echo "设置历史终端操作记录的长度，及它保留的历史记录的格式"
found=`grep -c HISTTIMEFORMAT /etc/profile`
if ! [ $found -gt "0" ]
then
echo "export HISTSIZE=2000" >> /etc/profile
echo "export HISTTIMEFORMAT='%F %T:'" >> /etc/profile
fi


echo "添加高速的阿里云的epel源"
if [ -f /etc/yum.repos.d/epel.repo ];then
echo "/etc/yum.repos.d/epel.repo已存在，无需操作"
else
cat > /etc/yum.repos.d/epel.repo << EOF
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=http://mirrors.aliyun.com/epel/7/$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
 
[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
baseurl=http://mirrors.aliyun.com/epel/7/$basearch/debug
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0
 
[epel-source]
name=Extra Packages for Enterprise Linux 7 - $basearch - Source
baseurl=http://mirrors.aliyun.com/epel/7/SRPMS
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0
EOF
fi

echo "初始化系统完成，如需添加功能请修改脚本"
